<?php
namespace Admin\Controller;

use Admin\Util\lib\API;
use Admin\Util\lib\YZTokenClient;
use Org\Util\String;
use Util\AccessController;
use Admin\Util\lib\YouZanApiFunction;

class BookController extends AccessController {

    public function AddAllBookIndex() {
        $ALLDATA = S('EIdata');
        $this->DATA = $ALLDATA;
        $this->display();
    }

    public function importXLS() {
        if (!isset($_FILES['execel']) || $_FILES["import"]["error"] != 0) {
            exit('File Not Exit!');
        }
        $extension = explode('.', $_FILES['execel']['name']);

        $res = \Admin\Util\ExcelUtil::importExecl($_FILES['execel']['tmp_name'], $extension[1]);
        S('EIdata', $res);
        $this->redirect('Book/importHandle');
    }

    public function importHandle() {
        set_time_limit(300);
        ini_set('memory_limit', '256M'); //升级为申请256M内存
        $res = S('EIdata');
        unset($res[0]);
        $AllData = array();
        foreach ($res as $key => $data1) {
            $data['PIC_URL'] = '';//图片路径
            $data['TITLE'] = $data1[0];//书名
            $data['ISBN'] = $data1[1];//条形码
            $data['AUTHOR'] = $data1[2];//作者
            $data['Y_PRICE'] = $data1[3];;//原价
            $data['PACKING_SPECIFICATION'] = $data1[4];//包装规格
            $data['PRESS'] = $data1[5];//出版社
            $data['PUBLICATION_DATE'] = $data1[6];;//出版日期

            $data['BOOK_SIZE'] = $data1[7];//书籍开本
            $data['region_one'] = $data1[8];//第一分类
            $data['region_two'] = $data1[9];//第一分类

            $data['STOCK'] = $data1[10];//库存
            $data['COST'] = $data1[11];//书籍成本
            $data['KG'] = $data1[12];//书籍重量
            $data['LEVEL'] = $data1[13];//成色新旧
            $data['AGE'] = $data1[14];//阅读年龄
            $data['PAGE'] = $data1[15]; //页数
            $data['LANGUAGES'] = $data1[16];//语言
            $data['RENT'] = $data1[17];//租金
            $data['ZN_PRICE'] = $data1[18];//滞纳金
            $data['XS_PRICE'] = $data1[19];//销售价格
            $data['YJ_READ_TIME'] = $data1[20];//预计阅读时间
            $data['RUKU_TIME'] = date('Y-m-d H:i:s');//入库时间
            $data['STATE'] = '未入库';//图书状态 1 在馆 2
            $AllData[$key - 1] = $data;
        }
        $LS = Service('Book', 'book');
        $OK = $LS->AddAll($AllData);
        S('EIdata', $AllData);
        if ($OK) {
            $this->redirect('Book/AddAllBookIndex');
        } else {
            $this->error('导入失败');
        }

    }

    public function clear() {
        S('EIdata', NULL);
        $this->redirect('Book/AddAllBookIndex');
    }

    public function Index() {
        $LS = Service('Book', 'book');
        $this->CSDATA = $_GET;

        $type_one = $LS->query(null, null, 'distinct region_one');
        $type_two = $LS->query(null, null, 'distinct region_two');

        //书籍成色
        $LS1 = Service('Book', 'region');
        $CSpdata = $LS1->find(array('region_name' => '书籍成色'));
        $CSsondata = $LS1->query(array('region_fid' => $CSpdata['region_id']));
        $this->BOOK_LEVEL = $CSsondata;

        //书籍包装
        $flpdata = $LS1->find(array('region_name' => '书籍包装'));
        $yjfldata = $LS1->query(array('region_fid' => $flpdata['region_id']));
        $this->BOOK_BZ = $yjfldata;

        //适读年龄段
        $flpdata = $LS1->find(array('region_name' => '智能选书年龄段'));
        $yjfldata = $LS1->query(array('region_fid' => $flpdata['region_id']));
        $this->BOOK_AGE = $yjfldata;


        $this->BOOK_LEVEL_JSD = $CSsondata;
        $this->CSDATA_JOIN = json_encode($_GET);

        $this->type_one = $type_one;
        $this->type_two = $type_two;
        $this->SGCODE = $_GET['SGCODE'] ? $_GET['SGCODE'] : 0;
        $this->banner_id = $_GET['banner_id'] ? $_GET['banner_id'] : 0;
        $this->SS = $_GET['SS'] ? $_GET['SS'] : 0;
        $this->TZ = $_GET['TZ'] ? $_GET['TZ'] : 0;
        $this->BY = $_GET['BY'] ? $_GET['BY'] : 0;

        //搜索字段
        $_GET['region_one'] ? $where['region_one'] = array('like',
            '%' . $_GET['region_one'] . '%') : 1;
        $_GET['region_two'] ? $where['region_two'] = array('like',
            '%' . $_GET['region_two'] . '%') : 1;
        $_GET['TITLE'] ? $where['TITLE'] = array('like',
            '%' . $_GET['TITLE'] . '%') : 1;
        $_GET['ISBN'] ? $where['ISBN'] = $_GET['ISBN'] : 1;
        $_GET['AUTHOR'] ? $where['AUTHOR'] = array('like',
            '%' . $_GET['AUTHOR'] . '%') : 1;
        $_GET['PRESS'] ? $where['PRESS'] = array('like',
            '%' . $_GET['PRESS'] . '%') : 1;
        $_GET['PACKING_SPECIFICATION'] ? $where['PACKING_SPECIFICATION'] = $_GET['PACKING_SPECIFICATION'] : 1;
        $_GET['AGE'] ? $where['AGE'] = array('like',
            '%' . $_GET['AGE'] . '%') : 1;
        $_GET['STATE'] ? $where['STATE'] = $_GET['STATE'] : 1;
        $_GET['SXJ'] == -1 ? $where['SXJ'] = 0 : 1;
        $_GET['SXJ'] == 1 ? $where['SXJ'] = 1 : 1;
        $_GET['IS_BLACK'] == -1 ? $where['IS_BLACK'] = 0 : 1;
        $_GET['IS_BLACK'] == 1 ? $where['IS_BLACK'] = 1 : 1;


        $count = $LS->count($where);
        $Page = new \Think\NewPage($count, 5);
        $show = $Page->show(); // 分页显示输出
        $limit = $Page->limit();
        $this->show = $show;
        $files = C('ADMIN_BOOK_LS');
        $order = I('get.order') != '' && I('get.sort') != '' ? PageSort(I('get.order'), I('get.sort')) : 'Y_PRICE asc';
        $DATA = $LS->query($where, $limit, $files, $order);

        if ($_GET['ISBN']) {
            $files = 'ISBN,Title as TITLE,Writer as AUTHOR ,publish as PRESS,id as booklistid';
            $LS1 = Service('Book', 'booklist');
            $DATA1 = $LS1->query(array('ISBN' => $_GET['ISBN']), null, $files);
            $count1 = $LS1->count(array('ISBN' => $_GET['ISBN']));
            if ($count1 > 0) {
                $DATA = array_merge($DATA, $DATA1);
            }

        }


        $this->DATA = $DATA;
        $this->display();
    }

    //    public function pageLoad_self() {
    //        Util("LoadPage");
    //        $page = new \Util\LoadPage($_POST);
    //        $limit = $page->limit($_POST['page']);
    //        $order = 'LOOK_NUM DESC';//默认排序
    //        $_GET['RED'] == 1 ? $order = 'TITLE DESC' : 1;//书名重复时的排序
    //        $_GET['RED'] == 2 ? $order = 'ISBN DESC' : 1;//ISBN重复时的排序
    //
    //        unset($_GET['SGCODE']);
    //        unset($_GET['banner_id']);
    //        unset($_GET['SS']);
    //        unset($_GET['TZ']);
    //        unset($_GET['BY']);
    //        if ($_GET['sortNAME'] && $_GET['isDESC']) {
    //            $order = $_GET['sortNAME'] . ' ' . $_GET['isDESC'];
    //            unset($_GET['isDESC']);
    //            unset($_GET['sortNAME']);
    //        }
    //        $WEHE = LIKEGET_ADMIN($_GET);
    //
    //        $files = C('ADMIN_BOOK_LS');
    //
    //        //是否入库
    //        $_GET['STATE'] ? $WEHE['STATE'] = $_GET['STATE'] : 1;
    //        //上下架
    //        $_GET['SXJ'] == -1 ? $WEHE['SXJ'] = 0 : 1;
    //        $_GET['SXJ'] == 1 ? $WEHE['SXJ'] = 1 : 1;
    //
    //        //包装
    //        $_GET['PACKING_SPECIFICATION'] ? $WEHE['PACKING_SPECIFICATION'] = $_GET['PACKING_SPECIFICATION'] : 1;
    //        $LS = Service('Book', 'book');
    //        $_GET['ID'] ? $WEHE['ID'] = $_GET['ID'] : 1;
    //        $DATA = $LS->query($WEHE, $limit, $files, $order);
    //        $count = $LS->count($WEHE);
    //        if ($_GET['ISBN']) {
    //            $files = 'ISBN,Title as TITLE,Writer as AUTHOR ,publish as PRESS,id as booklistid';
    //            $LS1 = Service('Book', 'booklist');
    //            $DATA1 = $LS1->query(array('ISBN' => $_GET['ISBN']), null, $files);
    //            $count1 = $LS1->count(array('ISBN' => $_GET['ISBN']));
    //            if ($count1 > 0) {
    //                $DATA = array_merge($DATA, $DATA1);
    //                $count += $count1;
    //            }
    //
    //        }
    //        echo $page->getRes($DATA, $count);
    //    }

    public function AddBookIndex() {
        $LS = Service('Book', 'book');
        $data = $LS->find(array('ID' => $_GET['ID']));

        $this->BOOK_LANGUAGES = C('BOOK_LANGUAGES');
        $this->BOOK_PACKING = C('BOOK_PACKING');
        $this->BOOK_LEVEL = C('BOOK_LEVEL');
        $this->BOOK_STATE = C('BOOK_STATE');

        $LS = Service('Book', 'region');
        $flpdata = $LS->find(array('region_name' => '童书分类'));
        $yjfldata = $LS->query(array('region_fid' => $flpdata['region_id']));
        $this->yjfldata = $yjfldata;

        $this->DATA = $data;
        $this->display('AddBookIndex');
    }

    /*
     * 添加书籍
     */
    public function AddHanld() {
        $save = $_POST;
        $save['RUKU_TIME'] = date('Y-m-d H:i:s');
        $LS = Service('Book', 'book');
        $OK = $LS->insert($save);
        $OK ? $this->success('添加成功') : $this->error('添加失败');
    }

    /*
     * 编辑书籍index
     */
    public function EditBookIndex() {
        $LS = Service('Book', 'book');
        $data = $LS->find(array('ID' => $_GET['ID']));


        $this->BOOK_STATE = C('BOOK_STATE');

        //分类
        $LS1 = Service('Book', 'region');
        $flpdata = $LS1->find(array('region_name' => '童书分类'));
        $yjfldata = $LS1->query(array('region_fid' => $flpdata['region_id']));
        $this->yjfldata = $yjfldata;

        //语言
        $YYpdata = $LS1->find(array('region_name' => '语言'));
        $YYsondata = $LS1->query(array('region_fid' => $YYpdata['region_id']));
        $this->BOOK_LANGUAGES = $YYsondata;

        //年龄段
        $YYpdata = $LS1->find(array('region_name' => '智能选书年龄段'));
        $YYsondata = $LS1->query(array('region_fid' => $YYpdata['region_id']));
        $this->BOOK_AGE = $YYsondata;

        //书籍成色
        $CSpdata = $LS1->find(array('region_name' => '书籍成色'));
        $CSsondata = $LS1->query(array('region_fid' => $CSpdata['region_id']));
        $this->BOOK_LEVEL = $CSsondata;

        //书籍包装
        $BZpdata = $LS1->find(array('region_name' => '书籍包装'));
        $BZsondata = $LS1->query(array('region_fid' => $BZpdata['region_id']));
        $this->BOOK_PACKING = $BZsondata;


        //书籍价格统一
        $this->BookPrice($_GET['ID']);

        //仓库位置
        $LS2 = Service('Book', 'ck');
        $ckdata = $LS2->query();
        $this->CKDATA = $ckdata;

        //所有书籍规格
        $LS3 = Service('Book', 'book_gg');
        $AllBookGG = $LS3->query(array('BOOK_ID' => $_GET['ID']));
        $this->AllBookGG = $AllBookGG;


        //更新数据
        $data = $LS->find(array('ID' => $_GET['ID']));


        //书籍标签
        $flpdata = $LS1->find(array('region_name' => '书籍标签'));
        $yjfldata = $LS1->query(array('region_fid' => $flpdata['region_id']));
        $this->BOOK_BQ = $yjfldata;

        //显示书籍标签
        $LS2 = Service('Book', 'book_bq');
        $bqdata = $LS2->query(array('BOOK_ID' => $_GET['ID']), null, null, null, 'ts_region on ts_region.region_id = ts_book_bq.BQ_ID ');
        $this->BQDATA = $bqdata;
        $this->BQDATA_JSON = json_encode($bqdata);


        //默认书柜位置
        $LS4 = Service('Book', 'sg');
        $MRWZDATA = $LS4->GetWZ1($this->S('MRWZID'));
        $this->MRWZDATA = $MRWZDATA;
        $this->DATA = $data;
        $this->BHCODE = $_GET['BHCODE'] ? $_GET['BHCODE'] : 0;

        $this->display();
    }


    /*
     * 编辑书籍
     */
    public function EditBook() {
        $LS = Service('Book', 'book');
        $update = $_POST;
        $_POST['PIC_URL']? $update['PIC_URL'] = $_POST['PIC_URL']:1;
        $update['details'] = preg_replace("/<img\s+[^>]*?src=(\'|\")(.*?)\\1[^>]*>/i", '<img src="$2" class="img-responsive center-block"/>', $_POST['CONTENT']);
        $LS1 = Service('Book', 'region');
        $region = $LS1->find(array('region_id' => $_POST['region_one']));
        $update['region_one'] = $region['region_name'] ? $region['region_name'] : $_POST['region_one'];
        //修改
        if ($_POST['ID']) {
            $where['ID'] = $_POST['ID'];
            $OK = $LS->update($where, $update);
            $BOOK_ID = $_POST['ID'];
        } else {
            $OK = $LS->insert($update);
            $BOOK_ID = $OK;
        }
        //书籍价格统一
        $this->BookPrice($_POST['ID']);
        if ($OK) {
            //添加记录
            $log['ADMIN_ID'] = $this->S('ID');
            $log['BOOK_ID'] = $_POST['ID'];
            $log['TYPE'] = 3;
            $BL = A('Admin/BookLog');
            $BL->Add($log);
            $_GET['BHCODE'] ? $this->success('编辑/添加成功', U('StoreHouse/Bookindex?BHCODE=' . $_GET['BHCODE']))
                : $this->success('编辑成功', U('Book/EditBookIndex?ID=' . $BOOK_ID));
        } else {
            $_GET['BHCODE'] ? $this->error('编辑/添加失败', U('StoreHouse/Bookindex?BHCODE=' . $_GET['BHCODE']))
                : $this->success('编辑成功', U('Book/EditBookIndex?ID=' . $_POST['ID']));
        }


    }

    /*
     * 价格统一方法
     */
    public function BookPrice($ID) {
        $LS = Service('Book', 'book');
        $data = $LS->find(array('ID' => $ID));
        //书籍规格
        $LS3 = Service('Book', 'book_gg');
        $QXBOOK = $LS3->findBookGG(array('BOOK_ID' => $ID,
            'GG_NAME' => '全新'), $data);
        $ESBOOK = $LS3->findBookGG(array('BOOK_ID' => $ID,
            'GG_NAME' => '二手书'), $data);
        //修改书籍销售价
        $LS4 = Service('M', 'book');
        $where4['ID'] = $ID;
        $minWhere['BOOK_ID'] = $ID;
        $minWhere['NUM'] = array('gt',
            0);
        $min_PRICE = $LS3->query($minWhere, null, 'min(GG_PRICE) as min');
        if ($min_PRICE[0]['min']) {
            $save4['XS_PRICE'] = $min_PRICE[0]['min'];
        } else {
            $save4['XS_PRICE'] = $ESBOOK['GG_PRICE'];
        }
        $LS4->update($where4, $save4);
    }

    /*
     * 删除书籍
     */
    public function delete() {
        $where['ID'] = $_POST['ID'];
        $LS = Service('Book', 'book');
        $OK = $LS->remove($where);
        echo $OK;
    }

    /*
     * 查看书籍位置
     */
    public function GetWZ() {
        $LS = Service('Book', 'book');
        $str = $LS->GetWZ($_POST['ID']);
        echo $str;
    }

    /*
     * 图书入库
     */

    public function RuKu() {
        $API = new API();
        $OK = $API->RUKU($_POST['ID'], $_POST['SGCODE']);
        echo $OK;
    }

    /*
     * 获取二级子分类
     */
    public function GetRegion_two() {
        $region_id = $_POST['ID'];
        $LS = Service('Book', 'region');
        $data = $LS->query(array('region_fid' => $region_id));
        $this->ajaxReturn($data, 'JSON');
    }

    /*
     * 上下架书籍
     */
    public function updateShelves() {
        $where['ID'] = $_POST['ID'];
        $save['SXJ'] = $_POST['SXJ'];
        $save['SJ_TIME'] = date('Y-m-d H:i:s');
        $LS = Service('Book', 'book');
        $OK = $LS->update($where, $save);
        echo $OK;
    }


    public function updateSTOCK() {
        $where['ID'] = $_POST['ID'];
        $LS = Service('Book', 'book');
        $DATA = $LS->find($where);
        $save['STOCK'] = $DATA['STOCK'] + $_POST['top'];
        if ($save['STOCK'] < 0) {
            echo -1;
            die();
        }
        $OK = $LS->update($where, $save);
        if ($OK) {
            echo $save['STOCK'];
        } else {
            echo -2;
        }
    }

    /*
     * 书籍内图页面
     */
    public function EditBookPicIndex() {
        $LS = Service('Book', 'book_img');
        $data = $LS->query(array('BOOK_ID' => $_GET['ID']));
        $this->BOOK_ID = $_GET['ID'];
        $this->DATA = $data;
        $this->display();
    }

    /*
  * 书籍内图页面
  */
    public function EditBookPic() {
        $LS = Service('Book', 'book_img');
        $pic_url_s = $_POST['picpath'];
        foreach ($pic_url_s as $k => $v) {
            $temp['IMG_ID'] = 'B_P' . uniqid() . $k . rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9);
            $temp['BOOK_ID'] = $_POST['ID'];
            $temp['IMG_URL'] = C('ROOT_PIC_URL') . substr($v, 1, strlen($v));
            $save[] = $temp;
        }
        $OK = $LS->UpdatePic($_POST['ID'], $save);
        $OK ? $this->success('成功') : $this->error('失败');
    }

    /*
     * 添加副本
     */
    public function Add_fb() {
        $LS = Service('Book', 'book');
        $data1 = $LS->find(array('BOOK_ID' => $_POST['BOOK_ID']));
        if ($data1) {
            unset($data1['ID']);
            unset($data1['STOCK']);
            unset($data1['RUKU_TIME']);
            unset($data1['STATE']);
            unset($data1['SXJ']);
            unset($data1['SJ_TIME']);
            $data1['TITLE'] = $_POST['book_name_fb'];
            $data1['XS_PRICE'] = $_POST['XS_PRICE'];
            $data1['LEVEL'] = $_POST['book_name_LEVEL'];
            $addOK = $LS->insert($data1);
            $addOK ? $this->success('添加成功') : $this->error('添加失败');

        } else {
            $this->error('添加失败');
        }

    }

    /*
     * 获取书籍的位置 new
     * 根据规格id
     */
    public function GetBookWZ() {
        $LS = Service('Book', 'wz');
        $WZDATA = $LS->query(array('GG_ID' => $_POST['ID']));
        $alldata = array();
        foreach ($WZDATA as $k => $v) {
            $LS1 = Service('Book', 'sg');
            $temp = $LS1->GetWZ1($v['WZ_ID']);
            if ($temp) {
                $temp['BOOK_WZ_ID'] = $v['ID'];
                $alldata[] = $temp;
            }
        }
        $this->ajaxReturn($alldata, 'JSON');
    }

    /*
     * 移除位置
     */
    public function ycwz() {
        $LS = Service('Book', 'wz');
        //        $LS->ycwz(87);
        $OK = $LS->ycwz($_POST['ID']);
        echo $OK;
    }

    /*
     * 导入书籍到book
     */
    public function DRbook() {
        $LS = Service('Book', 'booklist');
        $data1 = $LS->find(array('id' => $_GET['id']));
        if ($data1) {
            $LS = Service('Book', 'book');
            $data['TITLE'] = $data1['Title'];//书名
            $data['ISBN'] = $data1['ISBN'];//条形码
            $data['AUTHOR'] = $data1['Writer'];//作者
            $data['Y_PRICE'] = $data1['Price'];;//原价
            $data['PRESS'] = $data1['publish'];//出版社
            $data['PUBLICATION_DATE'] = $data1['PublishDate'];;//出版日期
            $data['BOOK_SIZE'] = $data1['PageMode'];//书籍开本
            $data['PAGE'] = $data1['Pages']; //页数
            $data['RUKU_TIME'] = date('Y-m-d H:i:s');//入库时间
            $data['STATE'] = '未入库';//图书状态 1 在馆 2
            $OK = $LS->insert($data);
            $OK ? $this->success('导入成功') : $this->error('导入失败');
        } else {
            $this->error('导入失败');
        }
    }


    /*
     * 更新排序
     */
    public function updatesort() {
        $where['ID'] = $_POST['ID'];
        $save['SORT'] = $_POST['sortnum'];
        $LS = Service('Book', 'book');
        $finddata = $LS->find($where);
        if ($finddata) {
            $updateOK = $LS->update($where, $save);
            echo $updateOK;
        } else {
            echo 0;
        }
    }

    /*
     * 标签
     */
    public function SelectQB() {
        $LS1 = Service('Book', 'region');
        $BZpdata = $LS1->find(array('region_id' => $_POST['ID']));
        $BZsondata = $LS1->query(array('region_fid' => $BZpdata['region_id']));
        $this->ajaxReturn($BZsondata, "JSON");
    }

    /*
     * 添加标签
     */
    public function addbq() {

        $data = $_POST['allbq_ids'];
        $book_id = $_POST['book_id'];
        $LS1 = Service('Book', 'book_bq');
        foreach ($data as $k => $v) {
            $save['BOOK_ID'] = $book_id;
            $save['BQ_ID'] = $v;
            $OK = $LS1->find($save);
            if (!$OK) {
                $LS1->insert($save);
            }
        }
        $this->ajaxReturn($_POST['allbq_ids'], "JSON");
    }

    /*
     * 删除标签
     */
    public function DelBq() {
        $where['BQ_ID'] = $_POST['BQ_ID'];
        $where['BOOK_ID'] = $_POST['BOOK_ID'];
        $LS1 = Service('Book', 'book_bq');
        $OK = $LS1->remove($where);
        echo $OK;
    }

    /*
     * 套装列表
     */
    public function TzListIndex() {
        $LS = Service('M', 'tz_book_list');
        $where = null;
        $join = 'left join ts_book on ts_book.ID = ts_tz_book_list.BOOK_ID';
        $field = 'ts_book.*,ts_tz_book_list.ID as ID,ts_tz_book_list.BOOK_NUM as BOOK_NUM,ts_book.ID as book_id';
        $limit = null;
        $order = 'NAME';
        $group = 'ts_tz_book_list.ID ';
        $data = $LS->join($where, $join, $field, $limit, $order, $group);
        $LS1 = Service('M', 'tz_book');
        $red = 0;
        foreach ($data as $k => $v) {
            $notOKnum = 10000;
            $bookdata = $LS1->join(array('ts_tz_book.TZ_BOOK_LIST_ID' => $v['ID']), 'left join ts_book on ts_book.ID = ts_tz_book.BOOK_ID', 'ts_book.*', null, null);
            foreach ($bookdata as $k1 => $v1) {
                $v1['STOCK'] < $notOKnum ? $notOKnum = $v1['STOCK'] : 1;
            }
            $data[$k]['booknum'] = $v['BOOK_NUM'] - count($bookdata);
            $data[$k]['notOKnum'] = $notOKnum;
            $notOKnum > 0 ? $red++ : 1;
        }

        $this->RED = $red;
        $this->DATA = $data;
        $this->display();
    }

    /*
     * 添加套装书
     */
    public function TzBookListhandle() {
        $LS = Service('M', 'tz_book_list');
        if ($_POST['ID']) {
            $where['ID'] = $_POST['ID'];
            $OK = $LS->update($where, $_POST);
        } else {
            $OK = $LS->Add($_POST);
        }
        $OK ? $this->success('成功') : $this->error('失败');
    }

    /*
     * 删除套装书
     */
    public function TzBookListDel() {
        $LS = Service('M', 'tz_book_list');
        $OK = $LS->Del(array('ID' => $_POST['ID']));
        echo $OK;
    }

    /*
     * 套装书书籍列表
     */
    public function CtBookList() {
        $LS = Service('M', 'tz_book_list');
        $where = array('ts_tz_book_list.ID' => $_GET['ID']);
        $join = 'left join ts_book on ts_book.ISBN = ts_tz_book_list.ISBN';
        $files = 'ts_tz_book_list.*,ts_book.ID as book_id';
        $Listdata = $LS->join($where, $join, $files, null, null, 'ts_tz_book_list.ID ');
        $this->ListData = $Listdata[0];
        $LS = Service('M', 'tz_book');
        $where = array('ts_tz_book.TZ_BOOK_LIST_ID' => $_GET['ID']);
        $join = 'left join ts_book on ts_book.ID = ts_tz_book.BOOK_ID';
        $files = 'ts_book.*,ts_tz_book.ID as tz_book_id';
        $data = $LS->join($where, $join, $files, null, null);
        $this->count = $Listdata[0]['BOOK_NUM'] - count($data);
        $this->DATA = $data;

        //书籍包装
        $LS1 = Service('M', 'region');
        $flpdata = $LS1->find(array('region_name' => '书籍包装'));
        $yjfldata = $LS1->query(array('region_fid' => $flpdata['region_id']));
        $this->BOOK_BZ = $yjfldata;

        $this->display();
    }

    /*
     * 套装书添加书籍
     */
    public function TZ_AddBook() {
        $save = $_POST;
        $B_LS = Service('M', 'book');
        $B_OK = $B_LS->Add($save);
        $save1['TZ_BOOK_LIST_ID'] = $_POST['TZ_ID'];
        $save1['BOOK_ID'] = $B_OK;
        $TZ_LS = Service('M', 'tz_book');
        $TZ_LS->Add($save1);
        echo $B_OK;
    }

    /*
     * 移除套装书列表
     */
    public function DelTZBOOK() {
        $LS = Service('M', 'tz_book');
        $where['ID'] = $_POST['ID'];
        $OK = $LS->Del($where);
        echo $OK;
    }

    //添加完善套装书中的数据
    public function AddTzBook() {
        $LS = Service('M', 'tz_book');
        $OK = $LS->Add($_POST);
        echo $OK;
    }

    /*
     * 不使用
     * 辅助函数
     * 是否成套
     * 参数
     * 套装ID
     * top
     * 1=返回所有数据
     * 0=返回所有平台无书籍数据
     */
    function GetTzData($ID, $top = 1) {
        $LS = Service('M', 'tz_book_list');
        $LS1 = Service('M', 'book');
        $files = C('ADMIN_BOOK_LS');
        $data = $LS->find(array('ID' => $ID));
        $ISBNS = explode(',', $data['ISBN_LIST']);
        $res = array();
        $ER_NUM = array();
        foreach ($ISBNS as $k => $v) {
            $where['ISBN'] = $v;
            $where['STOCK'] = array('gt',
                0);
            $where['STATE'] = '入库';
            $temp = $LS1->find($where, $files);
            $temp ? 1 : $ER_NUM[] = $v;
            $temp['ISBN'] = $v;
            $res[] = $temp;
        }
        $ResData = $top ? $res : $ER_NUM;
        return $ResData;
    }

    /*
     * 套装书根据book_id 获取isbn列表后获取其中一个位置
     */
    public function GetTZBookWZ() {
        $LS1 = Service('M', 'book_wz_view');
        $where1['b_ID'] = $_POST['ID'];
        $join = 'ts_book on ts_book.ID = b_ID';
        $temp = $LS1->join($where1, $join);
        $this->ajaxReturn($temp, 'JSON');
    }

    /*
     * 套装书出库操作
     */
    public function TzCk() {
        $LS = Service('M', 'tz_book_list');
        $TZBOOKDATA = $LS->find(array('ID' => $_POST['TZID']));
        $DLS = explode(',', $TZBOOKDATA['DL_ISBN_LIST']);
        $DLS[] = $_POST['ISBN'];
        $DLSTR = join(",", $DLS);
        $UOK = $LS->update(array('ID' => $_POST['TZID']), array('DL_ISBN_LIST' => $DLSTR));
        if ($UOK) {
            $LS1 = Service('Book', 'wz');
            $OK = $LS1->ycwz($_POST['WE_ID']);
            if (!$OK) {
                $LS->update(array('ID' => $_POST['TZID']), array('DL_ISBN_LIST' => $TZBOOKDATA['DL_ISBN_LIST']));
                echo 0;
            } else {
                $OKDATA = $LS->find(array('ID' => $_POST['TZID']));
                $num1 = count(explode(',', $OKDATA['DL_ISBN_LIST']));
                $num2 = count(explode(',', $OKDATA['ISBN_LIST']));
                if ($num1 == $num2) {
                    $LS2 = Service('M', 'book');
                    $bookData = $LS2->find(array('ISBN' => $TZBOOKDATA['ISBN']));
                    $LS->update(array('ID' => $_POST['TZID']), array('DL_ISBN_LIST' => ''));
                    if ($bookData) {
                        echo $bookData['ISBN'];
                        exit;
                    } else {
                        echo 2;
                        exit;
                    }
                }
                echo $OK;
                exit;
            }
        } else {
            echo 0;
        }

    }

    /*
     * 添加副本
     */
    public function AddFBhandle() {
        $num = $_POST['NUM'];
        $WZID = $_POST['WZID'];
        //        $num = 3;
        //        $WZID = 6680;
        $LS = Service('M', 'wz');
        $wdata = $LS->find(array('ID' => $WZID));
        $successnum = 0;
        for ($i = 0; $i < $num; $i++) {
            $LS1 = Service('Book', 'wz');
            $OK = $LS1->AddWz($wdata['GG_ID'], $wdata['WZ_ID']);
            $OK ? $successnum++ : 1;
        }
        echo $successnum;
    }

    /*
     * 临时方法
     * 生成规格
     */
    public function createBookGG() {
        set_time_limit(0);
        $files = 'ID,PACKING_SPECIFICATION ,Y_PRICE';
        $LS = Service('M', 'book');
        $data = $LS->query(null, null, $files);
        $LS1 = Service('Book', 'book_gg');
        foreach ($data as $k => $v) {
            $LS1->findBookGG(array('BOOK_ID' => $v['ID'],
                'GG_NAME' => '全新'), $v);
            $LS1->findBookGG(array('BOOK_ID' => $v['ID'],
                'GG_NAME' => '二手书'), $v);
        }
    }

    /*
     * 临时方法
     * 出库位置转移
     */
    public function zy_wz() {
        set_time_limit(0);
        $LS = Service('M', 'wz');
        $sql = "SELECT BOOK_ID ,WZ_ID FROM ts_wz LEFT JOIN ts_book_gg on ts_book_gg.ID = ts_wz.GG_ID WHERE ts_book_gg.GG_NAME!='全新' and ts_book_gg.GG_NAME!='二手书' ORDER BY BOOK_ID ";
        $data = $LS->SQL($sql);
        $LS1 = Service('M', 'book_gg');
        $LS2 = Service('M', 'wz');
        foreach ($data as $k => $v) {
            $where['BOOK_ID'] = $v['BOOK_ID'];
            $where['GG_NAME'] = '二手书';
            $es = $LS1->find($where);
            $save['GG_ID'] = $es['ID'];
            $save['WZ_ID'] = $v['WZ_ID'];
            $LS2->Add($save);
        }
    }

    /*
     * 由ISBN获取书籍信息
     * json
     */
    public function GetBookToISBN() {
        $where['ISBN'] = $_POST['ISBN'];
        $LS = Service('M', 'book');
        $data = $_POST['ISBN'] ? $LS->find($where) : null;
        $this->ajaxReturn($data, 'JSON');
    }

    /*
    * 由书籍id获取书籍信息
    * json
    */
    public function GetBookToID() {
        $where['ID'] = $_POST['ID'];
        $LS = Service('M', 'book');
        $data = $LS->find($where);
        $this->ajaxReturn($data, 'JSON');
    }

    /*
     * 由书籍id获取书籍ISBN
     * 再由isbn获取书籍列表
     */
    public function GetBookListToIDInISBN(){
        $where['ID'] = $_POST['ID'];
        $LS = Service('M', 'book');
        $data = $LS->find($where);
        $res = $LS->query(array('ISBN'=>$data['ISBN']));
        $this->ajaxReturn($res, 'JSON');
    }

    /*
     * 添加套装书信息
     */
    public function AddTzBookList() {
        $LS2 = Service('M', 'tz_book_list');
        $LS1 = Service('M', 'book');
        if ($_GET['BOOK_ID']) {
            $where1['ID'] = $_GET['BOOK_ID'];
            $save1['ISBN'] = $_GET['ISBN'];
            $save1['TITLE'] = $_GET['TITLE'];
            $save1['TITLE_C'] = $_GET['TITLE_C'];
            $LS1->update($where1, $save1);
            $BOOK_ID = $_GET['BOOK_ID'];
        } else {
            $BOOK_ID = $LS1->Add($_GET);
        }
        $save2['BOOK_ID'] = $BOOK_ID;
        $save2['BOOK_NUM'] = $_GET['BOOK_NUM'];
        if ($_GET['ID']) {
            $where2['ID'] = $_GET['ID'];
            $LS2->update($where2, $save2);
        } else {
            $LS2->Add($save2);
        }

        if ($_GET['BOOK_ID']) {
            $this->success('编辑成功');
        } else {
            redirect(U('Book/EditBookIndex?ID=' . $BOOK_ID));
        }

    }

    /*
     * 添加新的规格
     *
     */
    public function AddNewGG() {
        $LS = Service('M', 'book_gg');
        $OK = $LS->Add($_POST);
        $OK ? $this->success('添加成功') : $this->error('添加失败');
    }

    /*
     * 删除规格
     */
    public function DelBookGG() {
        if ($_GET['NUM'] == 0) {
            $LS = Service('M', 'book_gg');
            $OK = $LS->Del(array('ID' => $_GET['ID']));
            $OK ? $this->success('删除成功') : $this->error('删除失败');
        } else {
            $this->error('有库存，暂时不能删除。请先出库');
        }
    }

    /*
     * 修改规格价格
     */
    public function updateBookGGpPrice() {
        $LS = Service('M', 'book_gg');
        $where['ID'] = $_POST['ID'];
        $save['GG_PRICE'] = $_POST['GG_PRICE'];
        $UOK = $LS->update($where, $save);
        if ($_POST['TYPE'] == 1) {
            echo $UOK;
        } else {
            $UOK ? $this->success('修改成功') : $this->error('修改失败');
        }
    }

    /*
   * 修改书籍原价
   */
    public function updateBookYPrice() {
        $LS = Service('M', 'book');
        $where['ID'] = $_POST['ID'];
        $save['Y_PRICE'] = $_POST['Y_PRICE'];
        $UOK = $LS->update($where, $save);
        if ($_POST['TYPE'] == 1) {
            echo $UOK;
        } else {
            $UOK ? $this->success('修改成功') : $this->error('修改失败');
        }
    }
}